package com.xyview.fast.modules.app.controller;

import com.google.code.kaptcha.Producer;
import com.xyview.fast.common.util.R;
import com.xyview.fast.modules.app.entity.UserEntity;
import com.xyview.fast.modules.app.service.SysCaptchaService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;

/**
 * 验证码生成测试
 */
@RestController
@RequestMapping("/app")
@Api(" 验证码生成测试")
@Slf4j
public class CaptchaController {
    @Autowired
    private SysCaptchaService sysCaptchaService;
    /**
     * 验证码
     */
    @GetMapping("captcha.jpg")
    @ApiOperation("获取验证码")
    public void captcha(HttpServletResponse response, String uuid)throws ServletException, IOException {
        response.setHeader("Cache-Control", "no-store, no-cache");
        response.setContentType("image/jpeg");
        log.info("传入的参数uuid--->"+uuid);
        log.info("传入的参数uuid:{}",uuid);
        //获取图片验证码
        BufferedImage image = sysCaptchaService.getCaptcha(uuid);

        ServletOutputStream out = response.getOutputStream();
        ImageIO.write(image, "jpg", out);
        IOUtils.closeQuietly(out);
    }
}
